package com.bdqn.spark.chapter05.action

import org.apache.spark.{SparkConf, SparkContext}

object Spark08_RDD_Operator_Action {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("action-aggregate")
    val sc = new SparkContext(sparkConf)

    val rdd = sc.makeRDD(List(1,2,3,4),2)
    // 行动算子

    //10 + 13 + 17 = 40
    // aggregateByKey : 初始值只会参与分区内计算
    // aggregate : 初始值会参与分区内计算,并且和参与分区间计算
    //val result: Int = rdd.aggregate(10)(_ + _, _ + _)

    // fold是aggregate的简化操作，分区内和分区间的计算逻辑一样，就可以用fold
    val result = rdd.fold(10)(_+_)

    println(result)
    sc.stop()
  }
}
